package com.weizhi.logscripts;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;

public class EmpiFileToSQL {
	
	public static String createSql(String empifileName) {
		String empiSQLfileName = empifileName.replace("empi.txt", "empiSQL.txt");
    	File empisqlfile = new File(empiSQLfileName);
    	if(empisqlfile.exists()){
			DeleteDirectory.deleteDir(empisqlfile);
		}
    	
    	
    	String result = "";
        File file = new File(empifileName);
        if(!file.exists()){
        	return null;
        }
        BufferedReader reader = null;
        try {
//        	empisqlfile.createNewFile();
            System.out.println("以行为单位读取文件内容，一次读一整行：");
            reader = new BufferedReader(new FileReader(file));
            String tempString = null;
            while ((tempString = reader.readLine()) != null) {
            	//单独处理empi
            	sql( empiSQLfileName, tempString);
            }
            reader.close();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e1) {
                }
            }
        }
        return result ;
    }
	
	public static void sql(String empiSQLfileName,String tempString){
		String[] strs = tempString.trim().split(":");
		String empi_str = strs[1].trim();
		int i = empi_str.indexOf(" ");
		String empi = empi_str.substring(0, i).trim();
		String other = empi_str.substring(i+1, empi_str.length()).trim();
		String result = "("+empi+",'"+other+"'),";
		AppendToFile.appendMethodB(empiSQLfileName, result+"\n");
	}

}
